package kks;

import static org.junit.Assert.assertEquals;
import kks.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.BattlefieldSpecification;

/**
 * Verifies that the CounterAll moves along the wall most of the time.
 * 
 * @author Kelli Sawai
 * 
 */
public class TestWallMovement extends RobotTestBed {

  BattlefieldSpecification field = new BattlefieldSpecification();
  // Height of the battlefield
  double fieldHeight = field.getHeight();
  // Width of the battlefield
  double fieldWidth = field.getWidth();

  // Number of movements against/near a wall
  double wallMovement = 0.0;
  // Number of movements not against/near a wall
  double otherMovement = 0.0;

  /**
   * Specifies that SpinBot and CounterAll are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SpinBot,kks.CounterAll";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Checks to see if robots gun ever points towards the wall it is adjacent to.
   * 
   * @param event information stored about the robots at the end of a turn
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // IRobotSnapshot robot creation taken from David Mau
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double robotX = robot.getX();
    double robotY = robot.getY();

    // robot along left side of battlefield
    if (robotX < 20) {
      wallMovement++;
    }

    // robot along right side of battlefield
    else if (robotX > fieldWidth - 20) {
      wallMovement++;
    }

    // robot along top side of battlefield
    else if (robotY > fieldHeight - 20) {
      wallMovement++;
    }

    // robot along bottom side of battlefield
    else if (robotY < 20) {
      wallMovement++;
    }

    else {
      otherMovement++;
    }
  }

  /**
   * The actual test, which asserts that CounterAll is moving near a wall at least 50% of the time.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertEquals("Check if CounterAll is against a wall.",
        (wallMovement / (otherMovement + wallMovement)) > .5, true);

  }
}
